<?php
class client extends Controller
{
	function __constructor()
	{
		parent::__constructor();

		
	}
	
	
	function index()
	{
		is_logged_in();
		$this->load->view("client/index");
	}
	
	function newclient()
	{
		is_logged_in();
		$this->load->model("model_company");
		$this->load->model("model_lu");
		
		$msg = "";
		$states = $this->model_lu->get("states");
		$name = $this->input->post("client_name");
		if($name != "")
		{
			$company = array(
							"company_name" => $this->input->post("client_name")
							,"company_contact1" => $this->input->post("client_contact1")
							,"company_contact2" => $this->input->post("client_contact2")
							,"company_phone1" => $this->input->post("client_phone1")
							,"company_phone2" => $this->input->post("client_phone2")
							,"company_address1" => $this->input->post("client_address1")
							,"company_suburb" => $this->input->post("client_suburb")
							,"company_state" => $this->input->post("client_state")
							,"company_postcode" => $this->input->post("client_postcode")
							,"company_email" => $this->input->post("client_email")
							,"charge_rate" => $this->input->post("charge_rate")
							,"interval" => $this->input->post("interval") 
							,"company_parent" => $_SESSION['user_company'] 
			);
			
			$this->load->model("model_company");
			$this->model_company->newCompany($company);
			$msg = $name . " Added";
		}
		$data = array(	"crum" => build_crum( array("Client", "New Client") )
						,"states" => $states
					  ,"msg" => $msg);
		$this->load->view("client/newclient",$data);
	}

	function editclient()
	{
		is_logged_in();
		$this->load->model("model_company");
		$this->load->model("model_lu");
		
		$msg = "";
		$states = $this->model_lu->get("states");
		$id = $this->uri->segment(3);
		$update_id = $this->input->post("client_id");
		
		if($update_id != "")
		{
			$company = array(
							"company_name" => $this->input->post("client_name")
							,"company_contact1" => $this->input->post("client_contact1")
							,"company_contact2" => $this->input->post("client_contact2")
							,"company_phone1" => $this->input->post("client_phone1")
							,"company_phone2" => $this->input->post("client_phone2")
							,"company_address1" => $this->input->post("client_address1")
							,"company_suburb" => $this->input->post("client_suburb")
							,"company_state" => $this->input->post("client_state")
							,"company_postcode" => $this->input->post("client_postcode")
							,"company_email" => $this->input->post("client_email")
							,"charge_rate" => $this->input->post("charge_rate")
							,"interval" => $this->input->post("interval") 
			);
			
			$this->model_company->updateCompany($company, $update_id);
			$msg = $name . " Updated";
			redirect("client/showallclients/");	
		}
		$data = array(	"crum" => build_crum( array("Client", "Edit Client") )
						,"client" => $this->model_company->getClient($id)
						,"states" => $states
					    ,"msg" => $msg);
					  
		$this->load->view("client/editclient",$data);
	}
	
	function showallclients()
	{

		is_logged_in();
		$this->load->model("model_company");
		$this->load->model("model_lu");
		
		$states = $this->model_lu->get("states");
		$this->load->model("model_company");
		
		$data = array(	"crum" => build_crum( array("Client", "Search") )
					  	,"clients" => $this->model_company->getAll()
						);
		
		$this->load->view("client/showallclients", $data);	
	}
	
	
}